package cn.example.smartagriculture.slice;

import cn.example.smartagriculture.bean.Account;
import cn.example.smartagriculture.net.Wan;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import cn.example.smartagriculture.ResourceTable;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import poerty.jianjian.converter.gson.GsonConverterFactory;
import poetry.jianjia.Call;
import poetry.jianjia.Callback;
import poetry.jianjia.JianJia;
import poetry.jianjia.Response;

public class LoginAbilitySlice extends AbilitySlice {
    private Button btnLogin;
    private TextField tfAccount, tfPassword;
    private JianJia mJianJia;
    private Wan mWan;
    private String TAG = "智慧农业";
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        btnLogin = (Button) findComponentById(ResourceTable.Id_btnLogin);
        tfAccount = (TextField) findComponentById(ResourceTable.Id_tfAccount);
        tfPassword = (TextField) findComponentById(ResourceTable.Id_tfPassword);

        btnLogin.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("测试");
                login(tfAccount.getText().trim(), tfPassword.getText().trim());
            }
        });
        mJianJia = new JianJia.Builder()
                .baseUrl("http://api.nlecloud.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mWan = mJianJia.create(Wan.class);
    }

    private void login(String account, String password) {
        System.out.println("账号：" + account + "\n" + "密码：" + password);
        mWan.login(account, password, true).enqueue(new Callback<Account>() {
            @Override
            public void onResponse(Call<Account> call, Response<Account> response) {
                try {
                    if(response.isSuccessful() && response.body().Status == 0) {
                        System.out.println(TAG + "AccessToken: " + response.body().ResultObj.AccessToken);
                        goToMainAbility();
                    } else {
                        System.out.println(TAG + "登录出错" + response.body().Msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<Account> call, Throwable throwable) {
                System.out.println(TAG + "登录请求出错" + throwable.getMessage());

            }
        });
    }

    private void goToMainAbility() {
        System.out.println(TAG + "前往主界面");
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("cn.example.smartagriculture")
                .withAbilityName("cn.example.smartagriculture.MainAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }
}